﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Otherlisttable_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Code").ToString.Equals("0") Then

                hidID.Value = Request.QueryString("Code")
                getLoai()
                getNgonngu()
                ShowData()
            End If
        End If
    End Sub
    Protected Sub getNgonngu()
        ddlLanguageId.Items.Clear()
        ddlLanguageId.AppendDataBoundItems = True
        ddlLanguageId.Items.Add(New ListItem("---Chọn---", ""))
        Using data As New VSEntities
            Dim p = (From q In data.Languages Select q.LanguageId, q.Name).ToList
            ddlLanguageId.DataValueField = "LanguageId"
            ddlLanguageId.DataTextField = "Name"
            ddlLanguageId.DataSource = p
            ddlLanguageId.DataBind()
            ddlLanguageId.SelectedValue = 0
        End Using
    End Sub
    Protected Sub getLoai()
        ddlTypeId.Items.Clear()
        ddlTypeId.AppendDataBoundItems = True
        ddlTypeId.Items.Add(New ListItem("---Chọn---", ""))
        Using data As New VSEntities
            Dim p = (From q In data.OtherListTableTypes Select q.Id, q.Name).ToList
            ddlTypeId.DataValueField = "Id"
            ddlTypeId.DataTextField = "Name"
            ddlTypeId.DataSource = p
            ddlTypeId.DataBind()
            ddlTypeId.SelectedValue = 0
        End Using
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As Otherlisttable = (From q In Data.Otherlisttables Where q.OLCode = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtOlcode.Text = IIf(IsNothing(p.OLCode) = True, "", p.OLCode)
                txtOtherlistname.Text = IIf(IsNothing(p.OtherListName) = True, "", p.OtherListName)
                'txtSyslist.Text = IIf(IsNothing(p.SysList) = True, "", p.SysList)
                'txtValuetype.Text = IIf(IsNothing(p.ValueType) = True, "", p.ValueType)
                txtNote.Text = IIf(IsNothing(p.Note) = True, "", p.Note)
                ddlTypeId.SelectedValue = IIf(IsNothing(p.TypeId) = True, "", p.TypeId)
                ddlLanguageId.SelectedValue = IIf(IsNothing(p.LanguageId) = True, "", p.LanguageId)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As Otherlisttable = (From q In Data.Otherlisttables Where q.OLCode = hidID.Value).SingleOrDefault
            Try
                p.OLCode = hidID.Value
                p.OtherListName = txtOtherlistname.Text.Trim()
                p.Note = txtNote.Text.Trim()
                p.TypeId = ddlTypeId.SelectedValue
                p.LanguageId = ddlLanguageId.SelectedValue
                Data.SaveChanges()
                Insert_App_Log("Update Otherlisttable:" & p.OLCode & "", Function_Name.OtherListTable, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Otherlisttable/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ShowData()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
